﻿using DotNetCore.CAP;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using O.M.Production.Application.IService;
using O.M.Production.Application.Service;
using O.M.Production.Domain.DTO.Produce_work_order;
using O.M.Production.Domain.Entity;
using O.M.Production.ErrorCodeEnum;
using System.Linq.Expressions;

namespace O.M.Production.Read.API.Controllers
{
    /// <summary>
    /// Api控制器-生产工单
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class Read_Produce_Work_OrderController : ControllerBase
    {
        private readonly I_produce_work_order_Service _produce_work_order_service;

        public Read_Produce_Work_OrderController(I_produce_work_order_Service produce_work_order_service)
        {
            _produce_work_order_service = produce_work_order_service;
        }
        /// <summary>
        /// 分页查询-生产工单
        /// </summary>
        /// <param name="PWOId_Name">工单编号或名称</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">页码</param>
        /// <returns>统一返回格式</returns>
        [HttpGet]
        public async Task<IActionResult> Get_produce_work_order(string? PWOId_Name, int pageSize, int pageIndex)
        {
            try
            {
                var list = await _produce_work_order_service.Get_produce_work_order(PWOId_Name, pageSize, pageIndex);
                return Ok(list);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 导出列表到Excel-生产工单
        /// </summary>
        /// <returns>Excel文件</returns>
        [HttpGet]
        public async Task<IActionResult> Post_Derive_Excel()
        {
            try
            {
                var list = await _produce_work_order_service.GetShowInforAsync();
                var ms = new MemoryStream();
                ms.SaveAs(list);
                ms.Seek(0, SeekOrigin.Begin);
                return new FileStreamResult(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                {
                    FileDownloadName = "生产工单.xlsx"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 查实单个工单实体-反填
        /// </summary>
        /// <param name="work_order_id">工单编号</param>
        /// <returns>统一返回格式</returns>
        [HttpGet]
        public async Task<IActionResult> Get_produce_work_order_Model(string? work_order_id)
        {
            try
            {
                var res = await _produce_work_order_service.Get_produce_work_order_Model(work_order_id);
                return Ok(res);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 添加生产工单
        /// </summary>
        /// <param name="produce_Work_Order_DTO">生产工单DTO</param>
        /// <returns>统一返回格式</returns>
        [HttpPost]
        public async Task<IActionResult> Post_Produce_Work_Order_Add(produce_work_order_DTO produce_Work_Order_DTO)
        {
            try
            {
                var res = await _produce_work_order_service.Post_Produce_Work_Order_Add(produce_Work_Order_DTO);
                return Ok(res);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 批量删除-生产工单
        /// </summary>
        /// <param name="produce_Work_Order_DTOs">生产工单DTO</param>
        /// <returns>统一返回格式</returns>
        [HttpPost]
        public async Task<IActionResult> Post_Batch_Del_Work_Order(List<produce_work_order_DTO> produce_Work_Order_DTOs)
        {
            try
            {
                var res = await _produce_work_order_service.Post_Batch_Del_Work_Order_Add(produce_Work_Order_DTOs);
                return Ok(res);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改-生产工单
        /// </summary>
        /// <param name="produce_Work_Order_DTO">生产工单DTO</param>
        /// <returns>统一返回格式</returns>
        [HttpPost]
        public async Task<IActionResult> Post_Upd_Produce_Work_Order(produce_work_order_DTO produce_Work_Order_DTO)
        {
            try
            {
                var res = await _produce_work_order_service.Post_Upd_Produce_Work_Order(produce_Work_Order_DTO);
                return Ok(res);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改状态-工序流程
        /// </summary>
        /// <param name="Wid">工单编号</param>
        /// <param name="State">状态</param>
        /// <param name="produce_Work_Order_DTO">生产工单DTO</param>
        /// <returns>0关闭 1报工 2开工 3完成 4重启</returns>
        [HttpPut]
        public async Task<IActionResult> Put_Upd_State_Work_Order(int? Wid, int State, produce_work_order_DTO? produce_Work_Order_DTO)
        {
            try
            {
                var res= await _produce_work_order_service.Put_Upd_State_Work_Order(Wid, State, produce_Work_Order_DTO);
                return Ok(res);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
    }
}
